<?php
/**
 * Document file: Location Admin Controller
 *
 * @category Admin
 * @package  Controller
 * @author   sophy <sophy.prak@gmail.com>
 * @license  http://kooms.info Personal Lince
 * @link     (_blank, http://kooms.info)
 */

/**
 * Location Admin Controller
 *
 * @category Admin
 * @package  Controller
 * @author   sophy <sophy.prak@gmail.com>
 * @license  http://kooms.info Personal Lince
 * @link     (_blank, http://kooms.info)
 */
class AdminLocationsController extends AdminController
{
    /**
     * Location model
     * @var Location
     */
    protected $location;

    /**
     * Inject the models.
     *
     * @param Location $location is model Location
     */
    public function __construct(Location $location)
    {
        parent::__construct();
        $this->location = $location;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        // Grab all the groups
        $locations = $this->location->paginate(10);

        // Show the page
        return View::make('admin/locations/index', compact('locations'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function getCreate()
    {
        $provinces = Province::all();
        $defaulProvince = $this->getProvinceBySlug($provinces, 'phnom-penh');
        $branches = ProvincePlaces::getBranches($defaulProvince->id);

        // Show the page
        return View::make('admin/locations/create', compact('provinces', 'branches'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function postCreate()
    {
        $rules = array(
            'name'   => 'required|min:3',
            'logo' => 'mimes:jpeg,bmp,png,gif|max:3000',
            'email' => 'email',
            'website' => 'url'
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->passes()) {
            $file = Input::file('image');
            if ($file) {

                $originalName = $file->getClientOriginalName();
                $filename = Str::random(36) .'.'. File::extension($originalName);

                if ($file->move(base_path().'/public/uploads/images/locations/', $filename)) {
                    $this->location->image =  $filename;
                    Image::make(base_path().'/public/uploads/images/locations/'. $filename)
                    ->resize(220, null, true)
                    ->save(base_path().'/public/uploads/images/locations/thumb/'. $filename);
                }
            }

            $this->location->user_id = Auth::user()->id;
            $this->location->name = Input::get('name');
            $this->location->description = Input::get('description');
            $this->location->branch_id = Input::get('branch');

            //TODO: for tmp
            $this->location->image = Input::get('img');

            //$this->location->province_id = Input::get('province');

            // Address
            $this->location->house = Input::get('house');
            $this->location->street = Input::get('street');
            $this->location->sangkat = Input::get('commune');
            $this->location->khan = Input::get('khan');
            $this->location->phone = Input::get('phone');
            $this->location->email = Input::get('email');
            $this->location->website = Input::get('website');
            $this->location->lat = Input::get('lat');
            $this->location->lng = Input::get('lng');

            if ($this->location->save()) {
                // Redirect to the new blog post page
                return Redirect::to('admin/locations/' . $this->location->id . '/edit')->with('success', Lang::get('admin/places/messages.create.success'));
            }
            // Redirect to the place create page
            return Redirect::to('admin/locations/create')->with('error', Lang::get('admin/places/messages.create.error'));

        }
        // Form validation failed
        return Redirect::to('admin/locations/create')->withInput()->withErrors($validator);
    }
    /**
     * getEdit Get edit localtion
     *
     * @param Location $location location model
     *
     * @return void
     */

    public function getEdit($location)
    {
        $provinces = Province::all();
        $branchBank = $location->branch;
        //print_r($branch); die();
        //$defaulProvince = $this->getProvinceBySlug($provinces, 'phnom-penh');
        $branches = ProvincePlaces::getBranches($branchBank->province_id);

        return View::make('admin/locations/edit', compact('location', 'provinces', 'branches', 'branchBank'));
    }
    /**
     * postEdit Post edit location
     *
     * @param Localtion $location localtion model
     *
     * @return void no thing return
     */
    public function postEdit($location)
    {
        $rules = array(
            'name'   => 'required|min:3',
            'logo' => 'mimes:jpeg,bmp,png,gif|max:3000',
            'email' => 'email',
            'website' => 'url'
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->passes()) {

            $file = Input::file('image');
            if ($file) {

                $originalName = $file->getClientOriginalName();
                $filename = Str::random(36) .'.'. File::extension($originalName);

                if ($file->move(base_path().'/public/uploads/images/locations/', $filename)) {

                    // Delete old file
                    File::delete(base_path().'/public/uploads/images/locations/' . $location->image);
                    File::delete(base_path().'/public/uploads/images/locations/thumb/' . $location->image);

                    $location->image =  $filename;
                    Image::make(base_path().'/public/uploads/images/locations/'. $filename)
                    ->resize(220, null, true)
                    ->save(base_path().'/public/uploads/images/locations/thumb/'. $filename);
                }
            }
            $location->user_id = Auth::user()->id;
            $location->name = Input::get('name');
            $location->description = Input::get('description');
            $location->branch_id = Input::get('branch');

            //TODO: for tmp
            $location->image = Input::get('img');

            //$location->province_id = Input::get('province');

            // Address
            $location->house = Input::get('house');
            $location->street = Input::get('street');
            $location->sangkat = Input::get('commune');
            $location->khan = Input::get('khan');
            $location->phone = Input::get('phone');
            $location->email = Input::get('email');
            $location->website = Input::get('website');
            $location->lat = Input::get('lat');
            $location->lng = Input::get('lng');

            if ($location->save()) {
                // Redirect to the new blog post page
                return Redirect::to('admin/locations/' . $location->id . '/edit')->with('success', Lang::get('admin/places/messages.create.success'));
            }

            // Redirect to the place create page
            return Redirect::to('admin/locations/' . $location->id . '/edit')->with('error', Lang::get('admin/places/messages.update.error'));

        }
        // Form validation failed
        return Redirect::to('admin/locations/' . $location->id . '/edit')->withInput()->withErrors($validator);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param $location
     * @return Response
     */
    public function getDelete($location)
    {
        // Show the page
        return View::make('admin/locations/delete', compact('location'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param $location
     * @return Response
     */
    public function postDelete($location)
    {
        // Declare the rules for the form validation
        $rules = array(
            'id' => 'required|integer'
        );

        // Validate the inputs
        $validator = Validator::make(Input::all(), $rules);

        // Check if the form validates with success
        if ($validator->passes()) {
            $id = $location->id;
            $location->delete();

            // Was the place deleted?
            $location = Place::find($id);
            if (empty($location)) {
                // Redirect to the blog posts management page
                return Redirect::to('admin/locations')->with('success', Lang::get('admin/places/messages.delete.success'));
            }

        }
        // There was a problem deleting the blog post
        return Redirect::to('admin/locations')->with('error', Lang::get('admin/places/messages.delete.error'));
    }
    public function getBanks()
    {
        return Location::all();
    }

    private function getProvinceBySlug($provinces, $slug)
    {
        $tmp = '';
        foreach ($provinces as $p) {
            if ($p->slug === $slug) {
                $tmp = $p;
                break;
            }
        }

        return $tmp;
    }
}
